// LSL script generated: RLV-New.RLV_MENU.DV_RLV_Give.lslp Sun May  2 00:09:51 Westeuropäische Sommerzeit 2010
/*
################################################################################################################################# Included
0 9    B  Allow/prevent seeing the names of the people around : @shownames=<y/n>     
1 9    B  Allow/prevent seeing all the hovertexts : @showhovertextall=<y/n>     
2 9    B  Allow/prevent seeing the hovertexts on the HUD of the user : @showhovertexthud=<y/n>     
3 9    B  Allow/prevent seeing the hovertexts in-world : @showhovertextworld=<y/n>
#################################################################################################################################
*/
integer attachment = 1;
string cmdname;
key victim;
list gMenu = [];
list statesMenu = [0,0,0,0];
key ToucherID;
integer channel_dialog;
integer listen_id;
string msg = "Hover Test";
list objects = [];
list selected = [];
integer gPage;


list pageMenu(list in,integer page){
    list out;
    integer maxpages = (llGetListLength(in) / 9);
    (page = (page % (maxpages + 1)));
    (out = ["<<","Back",">>"]);
    integer _listcount = ((page * 9) + 9);
    integer _i = (_listcount - 9);
    for (_i; (_i < _listcount); (_i++)) {
        if (((_i <= llGetListLength(in)) && (llList2String(in,_i) != ""))) (out += llList2String(in,_i));
    }
    return out;
}

llRlvSay(string command){
    if ((attachment == 1)) {
        llOwnerSay(("@" + command));
    }
    else  {
        llSay(-1812221819,(((((cmdname + ",") + ((string)victim)) + ",") + "@") + command));
    }
}

MakeRLVState(){
    if ((attachment == 1)) {
        if ((llList2Integer(statesMenu,0) == 1)) llRlvSay("shownames=n");
        if ((llList2Integer(statesMenu,1) == 1)) llRlvSay("showhovertextall=n");
        if ((llList2Integer(statesMenu,2) == 1)) llRlvSay("showhovertexthud=n");
        if ((llList2Integer(statesMenu,3) == 1)) llRlvSay("showhovertextworld=n");
    }
}

makeMenu(){
    (gMenu = []);
    (objects = []);
    integer x = 0;
    string name = "";
    while ((x < llGetInventoryNumber(6))) {
        (name = llGetInventoryName(6,x));
        if ((name == llGetSubString(name,0,23))) {
            (objects += name);
        }
        (x++);
    }
    (x = 0);
    while ((x < llGetInventoryNumber(5))) {
        (name = llGetInventoryName(5,x));
        if ((name == llGetSubString(name,0,23))) {
            (objects += name);
        }
        (x++);
    }
    if ((llGetListLength(objects) < 10)) {
        (objects = (["Clear","Back","Attach"] + objects));
    }
    else  {
        (objects += ["Clear","Attach"]);
        (objects = pageMenu(objects,gPage));
    }
    (channel_dialog = (-1 * ((integer)("0x" + llGetSubString(((string)llGetKey()),-5,-1)))));
    (listen_id = llListen(channel_dialog,"",NULL_KEY,""));
    llDialog(ToucherID,msg,objects,channel_dialog);
    llSetTimerEvent(45.0);
}

integer randBetween(integer min,integer max){
    return ((integer)(llFrand(((float)(max - min))) + min));
}

Rezz(){
    if ((llGetListLength(objects) > 0)) {
        list inventory = objects;
        string text = "";
        string text2 = "";
        integer add = randBetween(10,1000);
        (text = ("#RLV/~DV" + ((string)add)));
        (text2 = ("~DV" + ((string)add)));
        llRlvSay("acceptpermission=add");
        llSleep(5.0);
        if ((attachment == 1)) {
            llGiveInventoryList(llGetOwner(),text,inventory);
        }
        else  {
            llGiveInventoryList(victim,text,inventory);
        }
        llSleep(10.0);
        llRlvSay("acceptpermission=rem");
        llRlvSay((("attachall:" + text2) + "=force"));
    }
}

list ListUnique(list lAll){
    integer i;
    list lFiltered = llList2List(lAll,0,0);
    integer iAll = llGetListLength(lAll);
    for ((i = 1); (i < iAll); (++i)) {
        if ((llListFindList(lFiltered,llList2List(lAll,i,i)) == -1)) {
            (lFiltered += llList2List(lAll,i,i));
        }
    }
    return lFiltered;
}


default {

    state_entry() {
        llAllowInventoryDrop(1);
    }

    
    listen(integer channel,string name,key id,string message) {
        llListenRemove(listen_id);
        llSetTimerEvent(0.0);
        if ((message == "<<")) {
            (gPage--);
            integer maxpages = (llGetListLength(objects) / 9);
            if ((gPage < 0)) {
                (gPage = maxpages);
            }
            makeMenu();
        }
        else  if ((message == ">>")) {
            (gPage++);
            makeMenu();
        }
        else  if ((message == "Clear")) {
            (selected = []);
            makeMenu();
        }
        else  if ((message == "Attach")) {
            Rezz();
            makeMenu();
        }
        else  if ((message == "Back")) llMessageLinked(-4,1337,"DV_MAIN_RLV",ToucherID);
        else  if ((~llListFindList(objects,((list)message)))) {
            (selected += message);
            (selected = ListUnique(selected));
            (msg = llList2CSV(selected));
            makeMenu();
        }
    }

    
    on_rez(integer on) {
        llAllowInventoryDrop(1);
    }

    
    link_message(integer sender,integer num,string str,key id) {
        if ((num == 1337)) {
            if ((str == llGetScriptName())) {
                llMessageLinked(-4,1338,llGetScriptName(),NULL_KEY);
                (ToucherID = id);
                makeMenu();
            }
        }
        if ((num == 1338)) {
            if ((str == "attachment")) (attachment = 1);
            else  if ((str == "device")) (attachment = 0);
            else  if ((str != llGetScriptName())) {
                llListenRemove(listen_id);
            }
        }
        if ((num == 1339)) {
            if ((str == "ON_REZ")) {
                MakeRLVState();
            }
            if ((str == "RESET")) {
                (statesMenu = [0,0,0,0]);
            }
        }
        if ((num == 1329)) {
            (victim = ((key)str));
            (cmdname = ((string)llGetKey()));
        }
    }

  	
  	timer() {
        llSetTimerEvent(0.0);
        llListenRemove(listen_id);
    }
}
